<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Evaluates the given XPath expression and returns a typed result if possible</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="domxpath.construct.html">« DOMXPath::__construct</a></li>
      <li style="float: right;"><a href="domxpath.query.html">DOMXPath::query »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.domxpath.html">DOMXPath</a></li>
    <li>Evaluates the given XPath expression and returns a typed result if possible</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="domxpath.evaluate" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">DOMXPath::evaluate</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">DOMXPath::evaluate</span> &mdash; <span class="dc-title">
   Evaluates the given XPath expression and returns a typed result if possible
  </span></p>

 </div>
 <div class="refsect1 description" id="refsect1-domxpath.evaluate-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>DOMXPath::evaluate</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$expression</code></span>, <span class="methodparam"><span class="type">?</span><span class="type"><span class="type"><a href="class.domnode.html" class="type DOMNode">DOMNode</a></span><span class="type"></span></span> <code class="parameter">$contextNode</code><span class="initializer"> = <strong><code>null</code></strong></span></span>, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$registerNodeNS</code><span class="initializer"> = <strong><code>true</code></strong></span></span>): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>

  <p class="para rdfs-comment">
   Executes the given XPath <code class="parameter">expression</code> and returns
   a typed result if possible.
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-domxpath.evaluate-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">expression</code></dt>

     <dd>

      <p class="para">
       The XPath expression to execute.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">contextNode</code></dt>

     <dd>

      <p class="para">
       The optional <code class="parameter">contextNode</code> can be specified for
       doing relative XPath queries. By default, the queries are relative to 
       the root element.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">registerNodeNS</code></dt>

     <dd>

      <p class="para">
       The optional <code class="parameter">registerNodeNS</code> can be specified to 
       disable automatic registration of the context node.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-domxpath.evaluate-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns a typed result if possible or a <span class="classname"><a href="class.domnodelist.html" class="classname">DOMNodeList</a></span> 
   containing all nodes matching the given XPath <code class="parameter">expression</code>. 
  </p>
  <p class="para">
   If the <code class="parameter">expression</code> is malformed or the
   <code class="parameter">contextNode</code> is invalid,
   <span class="methodname"><strong>DOMXPath::evaluate()</strong></span> returns <strong><code>false</code></strong>.
  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-domxpath.evaluate-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5427">
    <p><strong>示例 #1 Getting the count of all the english books</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$doc&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DOMDocument</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">load</span><span style="color: #007700">(</span><span style="color: #DD0000">'book.xml'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$xpath&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DOMXPath</span><span style="color: #007700">(</span><span style="color: #0000BB">$doc</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$tbody&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getElementsByTagName</span><span style="color: #007700">(</span><span style="color: #DD0000">'tbody'</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">item</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;our&nbsp;query&nbsp;is&nbsp;relative&nbsp;to&nbsp;the&nbsp;tbody&nbsp;node<br /></span><span style="color: #0000BB">$query&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'count(row/entry[.&nbsp;=&nbsp;"en"])'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$entries&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$xpath</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">evaluate</span><span style="color: #007700">(</span><span style="color: #0000BB">$query</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$tbody</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"There&nbsp;are&nbsp;</span><span style="color: #0000BB">$entries</span><span style="color: #DD0000">&nbsp;english&nbsp;books\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
There are 2 english books
</pre></div>
    </div>
   </div>
  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-domxpath.evaluate-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="methodname"><a href="domxpath.query.html" class="methodname" rel="rdfs-seeAlso">DOMXPath::query()</a> - Evaluates the given XPath expression</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>